package com.melon.entity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 返回值封装
 *
 * @author zhangqinglong@acoinfo.com
 * @version 1.0
 * @since 2020/3/20 11:05
 */
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 5976851645888286693L;

    public static final Integer SUCCESS_CODE = 200;


    private Integer status;

    private String message;

    private String fieldErrors;

    private T data;

    public Response() {
    }

    public Response(T data) {
        this.status = SUCCESS_CODE;
        this.message = "success";
        this.data = data;
    }

    public Response(T data, String message, String fieldErrors) {
        this.status = 10000;
        this.message = message;
        this.fieldErrors = fieldErrors;
        this.data = data;
    }

    public Response(Integer status, String message) {
        this.status = status;
        this.message = message;
        this.fieldErrors = message;
    }

    public Response(Integer status, String message, String fieldErrors, T data) {
        this.status = status;
        this.message = message;
        this.fieldErrors = fieldErrors;
        this.data = data;
    }

    public static <T> Response<T> ok(T data) {
        return new Response<T>(data);
    }

    public static Response<Map<String, Object>> ok(String name, Object data) {
        Map<String, Object> res = new HashMap<>();
        res.put(name, data);
        return new Response<Map<String, Object>>(res);
    }

    public static <T> Response<T> ok(Integer status, T data) {
        return new Response<T>(status, null, null, data);
    }

    public static <T> Response<T> ok() {
        return new Response<T>(SUCCESS_CODE, "success", null, null);
    }

    public static Response<String> okStr() {
        return new Response<String>(SUCCESS_CODE, null, "success", "success");
    }

    public static <T> Response<T> error() {
        return new Response<T>(10000, "接口请求失败！");
    }

    public static <T> Response<T> error(String message) {
        return new Response<T>(10000, message);
    }

    public static <T> Response<T> error(String message, String filedErrors) {
        return new Response<T>(10000, message, filedErrors, null);
    }

    public static <T> Response<T> error(Integer status, String message, String fieldErrors, T data) {
        return new Response<T>(status, message, fieldErrors, data);
    }

    public static <T> Response<T> error(Integer status, String message) {
        return new Response<T>(status, message);
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getFieldErrors() {
        return fieldErrors;
    }

    public void setFieldErrors(String fieldErrors) {
        this.fieldErrors = fieldErrors;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
